﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Data;
using BaseSet.Entity.Common.Text;


namespace WebSite.Admin.SystemSet
{
    public partial class AdminRoleConfig : System.Web.UI.Page
    {
        private List<PowerItem> piList;
        private List<FunctionItem> fiList;
        private List<Functions> fList;
        private List<PowerItem> rolePIList;
        private Role conRole;
        private string id = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"] + "";

            RoleHelper rhelper = new RoleHelper();
            conRole = rhelper.GetRole(id);

            if (!Page.IsPostBack)
            {
                LoadContent();
            }
        }


        #region 加载内容
        private void LoadContent()
        {
            if (conRole != null)
            {
                litName.Text = conRole.RoleName;

                LoadLists();

                List<Functions> topMenus = new List<Functions>();
                if (fList != null && fList.Count > 0)
                {
                    foreach (Functions fun in fList)
                    {
                        if (fun.ParentID == "0" && topMenus.Contains(fun) == false)
                        {
                            topMenus.Add(fun);
                        }
                    }
                }

                rptFun.DataSource = topMenus;
                rptFun.DataBind();

            }

        }
        #endregion

        #region 当顶级菜单绑定时
        protected void rptFun_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.AlternatingItem && e.Item.ItemType != ListItemType.Item)
            {
                return;
            }

            HiddenField hidFunId = e.Item.FindControl("hidFunId") as HiddenField;
            DataList rptSubFun = e.Item.FindControl("rptSubFun") as DataList;
            Repeater rptFunItem = e.Item.FindControl("rptFunItem") as Repeater;

            if (hidFunId != null && rptSubFun != null && rptFunItem != null)
            {
                string funID = hidFunId.Value.Trim();

                if (funID != null && funID.Length > 0)
                {
                    //查找下级fun
                    List<Functions> subMenus = new List<Functions>();
                    if (fList != null && fList.Count > 0)
                    {
                        foreach (Functions fun in fList)
                        {
                            if (fun.ParentID == funID && subMenus.Contains(fun) == false)
                            {
                                subMenus.Add(fun);
                            }
                        }

                    }


                    if (subMenus != null && subMenus.Count > 0) //一级菜单下存在子菜单
                    {
                        rptSubFun.DataSource = subMenus;
                        rptSubFun.DataBind();
                    }
                    else//菜单下为功能项
                    {
                        List<FunctionItem> funItemList = new List<FunctionItem>();
                        if (fiList != null && fiList.Count > 0)
                        {
                            foreach (FunctionItem fi in fiList)
                            {
                                if (fi.FunctionID == funID && funItemList.Contains(fi) == false)
                                {
                                    funItemList.Add(fi);
                                }
                            }
                        }

                        if (funItemList != null && funItemList.Count > 0) //一级菜单下存在子菜单
                        {
                            rptFunItem.DataSource = funItemList;
                            rptFunItem.DataBind();
                        }

                    }
                }
            }
        }
        #endregion

        #region 当二级菜单绑定时
        protected void rptSubFun_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.AlternatingItem && e.Item.ItemType != ListItemType.Item)
            {
                return;
            }

            HiddenField hidFunId = e.Item.FindControl("hidSubFunID") as HiddenField;
            Repeater rptFunItem = e.Item.FindControl("rptSubFunItem") as Repeater;

            if (hidFunId != null && rptFunItem != null)
            {
                string funID = hidFunId.Value.Trim();

                if (funID != null && funID.Length > 0)
                {
                    List<FunctionItem> funItemList = new List<FunctionItem>();
                    if (fiList != null && fiList.Count > 0)
                    {
                        foreach (FunctionItem fi in fiList)
                        {
                            if (fi.FunctionID == funID && funItemList.Contains(fi) == false)
                            {
                                funItemList.Add(fi);
                            }
                        }
                    }

                    if (funItemList != null && funItemList.Count > 0) //一级菜单下存在子菜单
                    {
                        rptFunItem.DataSource = funItemList;
                        rptFunItem.DataBind();
                    }
                }
            }

        }
        #endregion

        #region 绑定选择框
        protected void rptFunItem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.AlternatingItem && e.Item.ItemType != ListItemType.Item)
            {
                return;
            }

            HiddenField hidItemId = e.Item.FindControl("hidItemId") as HiddenField;
            CheckBoxList chklstFunItem = e.Item.FindControl("chklstFunItem") as CheckBoxList;


            if (hidItemId != null && chklstFunItem != null)
            {
                string itemId = hidItemId.Value.Trim();
                if (itemId == null || itemId.Length == 0)
                {
                    return;
                }
                #region 绑定二级菜单中功能菜单的 可配置功能

                //单一功能项
                chklstFunItem.Visible = true;

                ListItem li = new ListItem();

                if (piList != null && piList.Count > 0)
                {
                    if (piList.Find(delegate(PowerItem p) { return p.ItemID == itemId; }) != null)
                    {
                        li.Selected = true;
                    }

                }

                chklstFunItem.Items.Add(li);
                #endregion

            }

        }
        #endregion

        #region 加载List内容
        private void LoadLists()
        {
            if (piList == null || rolePIList == null)
            {
                //权限项
                PowerItemHelper piHelper = new PowerItemHelper();

                if (piList == null)
                {
                    piList = piHelper.GetUserPowerItem(id);
                }

                if (rolePIList == null)
                {
                    //rolePIList = piHelper.GetUserPowerItem(id);
                    rolePIList = piHelper.GetUserPowerItem("1");
                }

            }
            if (fiList == null)
            {
                //菜单项
                FunctionItemHelper fiHelper = new FunctionItemHelper();
                fiList = fiHelper.GetUserPowerFunItems(rolePIList);

            }

            if (fList == null)
            {
                //菜单
                FunctionsHelper funHelper = new FunctionsHelper();
                fList = funHelper.GetUserPowerFun(fiList);
            }

        }
        #endregion

        #region 提交
        protected void SureBtn_Click(object sender, EventArgs e)
        {
            PowerItemHelper piHelper = new PowerItemHelper();

            //加载List
            LoadLists();

            #region 读取权限项
            List<PowerItem> newPower = new List<PowerItem>();
            if (rptFun != null && rptFun.Items.Count > 0)
            {
                foreach (RepeaterItem rptFi in rptFun.Items)
                {
                    DataList rptSubFun = rptFi.FindControl("rptSubFun") as DataList;
                    Repeater rptFunItem = rptFi.FindControl("rptFunItem") as Repeater;

                    if (rptSubFun != null && rptSubFun.Items.Count > 0)
                    {
                        foreach (DataListItem rptSubFi in rptSubFun.Items)
                        {
                            rptFunItem = rptSubFi.FindControl("rptSubFunItem") as Repeater;
                            if (rptFunItem != null && rptFunItem.Items.Count > 0)
                            {
                                newPower = GetList(rptFunItem, newPower);
                            }
                        }
                    }
                    else if (rptFunItem != null && rptFunItem.Items.Count > 0)
                    {
                        newPower = GetList(rptFunItem, newPower);
                    }
                }
            }

            #endregion

            #region 删除原有项
            if (piList != null && piList.Count > 0)
            {
                foreach (PowerItem pi in piList)
                {
                    piHelper.DeletePowerItem(pi);
                }
            }
            #endregion

            #region 插入新增项
            if (newPower != null && newPower.Count > 0)
            {
                foreach (PowerItem pi in newPower)
                {
                    piHelper.AddPowerItem(pi);
                }
            }
            #endregion

            Tools.AlertAndRedirect("修改成功", "AdminRoleList.aspx");
          
        }
        #endregion

        private List<PowerItem> GetList(Repeater rpt, List<PowerItem> list)
        {
            if (list == null)
            {
                list = new List<PowerItem>();
            }
            if (rpt != null && rpt.Items.Count > 0)
            {
                foreach (RepeaterItem rpti in rpt.Items)
                {
                    HiddenField hidItemId = rpti.FindControl("hidItemId") as HiddenField;
                    CheckBoxList chklstFunItem = rpti.FindControl("chklstFunItem") as CheckBoxList;

                    PowerItem pi = new PowerItem();
                    string cs = string.Empty;
                    if (hidItemId != null)
                    {
                        string itemId = hidItemId.Value.Trim();

                        if (itemId == null || itemId.Length == 0)
                        {
                            continue;
                        }

                        if (chklstFunItem != null && chklstFunItem.Items.Count > 0) //item=1单一功能项
                        {
                            if (chklstFunItem.Items[0].Selected == true)
                            {
                                pi = new PowerItem();
                                pi.ItemID = itemId;
                                pi.RoleID = id;

                                list.Add(pi);
                            }

                        }
                    }

                }
            }

            return list;
        }

        #region 返回
        protected void ReturnBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminRoleList.aspx");     
        }
        #endregion
    }
}